﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2010
*/
#region Using
using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using Rio.Flow.Business.Repository.Translator;
using Rio.Flow.DataAccess;
using Rio.Doc.Business.Repository.Translator;

#endregion

namespace Rio.Flow.Business.Repository
{
    /// <summary>
    /// Message repository
    /// </summary>
    public class MessageFlowRepository : MessageFlowDer
    {
        #region Publics functions

        public static List<ParamMessageFlow> Load()
        {
            return ParamMessageFlowTranslator.TranslateParamMessageFlow(DA_ParamMessageFlow.Load());
        }

        /// <summary>
        /// Add a document on a message Flow
        /// </summary>
        /// <param name="p_lMessageFlowId">Identifier of the message flow</param>
        /// <param name="p_lDocumentId">Identifier of the document flow</param>
        public static ReturnBoolean AddDocumentToMessageFlow(long p_lMessageFlowId, long p_lDocumentId)
        {
            var l_oReturnBoolean = new ReturnBoolean();
            try
            {
                DA_MessageFlow.AddDocumentToMessageFlow(p_lMessageFlowId, p_lDocumentId);
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("Error in AddDocumentToMessageFlow (p_lMessageFlowId<" + p_lMessageFlowId + "> p_lDocumentId<" + p_lDocumentId + ">) -> Exception", new Exception(ex.Message));
                LogManager.Instance().LogError(l_oReturnBoolean.FullErrorMessage);
            }

            return l_oReturnBoolean;
        }

        /// <summary>
        /// Add a reminder on a message Flow
        /// </summary>
        /// <param name="p_lUserId">Identifier of the source user</param>
        /// <param name="p_lMessageFowId">Identifier of the Message flow</param>
        /// <param name="p_sGrpId">Group Identifier</param>
        public static List<ReturnBoolean> AddMessageFlowReminder(long p_lUserId, long p_lMessageFowId, long p_sGrpId)
        {
            var l_oReturnBooleans = new List<ReturnBoolean>();

            var l_oReturnBoolean = new ReturnBoolean();
            try
            {
                DA_MessageFlow.AddMessageFlowReminder(p_lUserId, p_lMessageFowId, p_sGrpId);
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("Error in AddMessageFlowReminder (p_lUserId<" + p_lUserId + "> p_lMessageFowId<" + p_lMessageFowId + "> p_sGrpId<" + p_sGrpId + ">) -> Exception", new Exception(ex.Message));
                LogManager.Instance().LogError(l_oReturnBoolean.FullErrorMessage);
            }

            l_oReturnBooleans.Add(l_oReturnBoolean);

            return l_oReturnBooleans;
        }

        public static long CountMessageFlowSentByMe(long p_lUserId)
        {
            return DA_MessageFlow.CountMessageFlowSentByMe(p_lUserId);
        }

        public static long CountMessageFlowInMyBox(long p_lUserId, long p_iDocState)
        {
            return DA_MessageFlow.CountMessageFlowInMyBox(p_lUserId, p_iDocState);
        }

        /// <summary>
        /// Get the message flow to fill the message flow SENTBOX
        /// </summary>
        /// <param name="p_lUserId">User ID</param>
        /// <returns>Return a list of Message flow</returns>
        public static List<MessageFlow> GetMessageFlowSentByMe(long p_lUserId) 
        {
            var l_oMessageFlows = new List<MessageFlow>();

            // Finaly traslate results into a list of documents
            l_oMessageFlows = MessageFlowTranslator.TranslateMessageFlows(DA_MessageFlow.GetMessageFlowSentByMe(p_lUserId));
            
            return l_oMessageFlows;
        }

        /// <summary>
        /// Get the message flow to fill the message flow INBOX
        /// </summary>
        /// <param name="p_lUserId">User ID</param>
        /// <returns>Return a list of Message flow</returns>
        public static List<MessageFlow> GetMessageFlowInMyBox(long p_lUserId)
        {
            var l_oMessageFlows = new List<MessageFlow>();

            // Finaly traslate results into a list of documents
            l_oMessageFlows = MessageFlowTranslator.TranslateMessageFlows(DA_MessageFlow.GetMessageFlowInMyBox(p_lUserId));

            return l_oMessageFlows;
        }

        /// <summary>
        /// Add a mssage Fow
        /// </summary>
        /// <param name="p_lEmetteurId">Identifier of the source user</param>
        /// <param name="p_lChronoId">Identifier of the chrono </param>
        /// <param name="p_lDocId">Document Identifier</param>
        /// <param name="p_sObjet">Object</param>
        /// <param name="p_sMemo">Text</param>
        /// <param name="p_sGroupDestinataire">All destination group seperate with a ";"</param>
        /// <param name="p_bSendAlertByMail"></param>
        /// <returns>The identifier of the message flow</returns>
        public static ReturnLong AddMessageFlow(long p_lEmetteurId, long p_lChronoId, long p_lDocId, string p_sObjet, string p_sMemo, string p_sGroupDestinataire, Boolean p_bSendAlertByMail, string p_sFolderURL = null)
        {
            var l_oReturnLong = new ReturnLong();

            try
            {
                l_oReturnLong.Value = DA_MessageFlow.AddMessageFlow(p_lEmetteurId, p_lChronoId, p_lDocId, p_sObjet, p_sMemo, p_sGroupDestinataire, p_bSendAlertByMail, p_sFolderURL);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("(p_lEmetteurId<" + p_lEmetteurId + "> p_lChronoId<" + p_lChronoId + "> p_lDocId<" + p_lDocId + "> p_sObjet<" + p_sObjet + "> p_sMemo<" + p_sMemo + "> p_sGroupDestinataire<" + p_sGroupDestinataire + ">p_bSendAlertByMail<" + p_bSendAlertByMail + ") -> Exception" + ex);
                l_oReturnLong.ErrorMessage = ex.Message;
                l_oReturnLong.Value = 0;
            }

            return l_oReturnLong;
        }

        /// <summary>
        /// Retreive historical message flow by an user
        /// </summary>
        /// <param name="p_lUserId">User ID</param>
        /// <param name="p_lDocumentId">Document ID</param>
        /// <param name="p_lMsgFlowId">MessageFlow ID</param>
        /// <returns>DataReader with historical</returns>
        public static List<StateMessageFlow> GetHistoryByUserMessageFlow(long p_lUserId, long p_lDocumentId, long p_lMsgFlowId)
        {
            var l_oReturnLong = new List<StateMessageFlow>();

            try
            {
                // Finaly traslate results into a list of documents
                l_oReturnLong = MessageFlowTranslator.TranslateStateMessageFlows(DA_MessageFlow.GetHistoryByUserMessageFlow(p_lUserId, p_lDocumentId, p_lMsgFlowId));
            }
            catch (Exception ex)
            {
                var l_oStateMessageFlow = new StateMessageFlow("(p_lUserId<" + p_lUserId + "> p_lDocumentId<" + p_lDocumentId + "> p_lMsgFlowId<" + p_lMsgFlowId + ">)", ex);
                LogManager.Instance().LogError(l_oStateMessageFlow.FullErrorMessage);
                l_oReturnLong.Add(l_oStateMessageFlow);
            }

            return l_oReturnLong;
        }

        /// <summary>
        /// Retreive historical message flow by a group
        /// </summary>
        /// <param name="p_lGroupId">Group ID</param>
        /// <param name="p_lDocumentId">Document ID</param>
        /// <param name="p_lMsgFlowId">MessageFlow ID</param>
        /// <returns>DataReader with historical</returns>
        public static List<StateMessageFlow> GetHistoryByGroupMessageFlow(long p_lGroupId, long p_lDocumentId, long p_lMsgFlowId)
        {
            var l_oReturnLong = new List<StateMessageFlow>();

            try
            {
                // Finaly traslate results into a list of documents
                l_oReturnLong = MessageFlowTranslator.TranslateStateMessageFlows(DA_MessageFlow.GetHistoryByGroupMessageFlow(p_lGroupId, p_lDocumentId, p_lMsgFlowId));
            }
            catch (Exception ex)
            {
                var l_oStateMessageFlow = new StateMessageFlow("(p_lGroupId<" + p_lGroupId + "> p_lDocumentId<" + p_lDocumentId + "> p_lMsgFlowId<" + p_lMsgFlowId + ">)", ex);
                LogManager.Instance().LogError(l_oStateMessageFlow.FullErrorMessage);
                l_oReturnLong.Add(l_oStateMessageFlow);
            }

            return l_oReturnLong;
        }

        /// <summary>
        /// Retreive historical message flow for all user's groupss
        /// </summary>
        /// <param name="p_lUserId">User ID</param>
        /// <param name="p_lDocumentId">Document ID</param>
        /// <param name="p_lMsgFlowId">MessageFlow ID</param>
        /// <returns>DataReader with historical</returns>
        public static List<StateMessageFlow> GetHistoryByUserGroupsMessageFlow(long p_lUserId, long p_lDocumentId, bool p_bIsAdmin)
        {
            var l_oStateMessageFlowList = new List<StateMessageFlow>();

            try
            {
                var l_oMessageFlowList = GetMessageFlowByDocId(p_lDocumentId);
               
                foreach (MessageFlow l_oMessageFlow in l_oMessageFlowList)
                {
                    // Finaly traslate results into a list of documents
                    var l_oStateMessageFlowListTemp = MessageFlowTranslator.TranslateStateMessageFlows(DA_MessageFlow.GetHistoryByUserGroupsMessageFlow(p_lUserId, p_lDocumentId, l_oMessageFlow.Identifier, p_bIsAdmin));
                    if (l_oStateMessageFlowListTemp != null && l_oStateMessageFlowListTemp.Count > 0)
                    {
                        foreach (StateMessageFlow l_oStateMessageFlow in l_oStateMessageFlowListTemp)
                        {
                            l_oStateMessageFlow.MessageFlowTitle = l_oMessageFlow.Objet;
                            l_oStateMessageFlowList.Add(l_oStateMessageFlow);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var l_oStateMessageFlow = new StateMessageFlow("(p_lUserId<" + p_lUserId + "> p_lDocumentId<" + p_lDocumentId + ">)", ex);
                LogManager.Instance().LogError(l_oStateMessageFlow.FullErrorMessage);
                l_oStateMessageFlowList.Add(l_oStateMessageFlow);
            }

            return l_oStateMessageFlowList;
        }

        /// <summary>
        /// Retreive all doc state
        /// </summary>
        /// <returns>DataReader with historical</returns>
        public static List<StateDoc> GetStateDocList()
        {
            var l_oReturnLong = new List<StateDoc>();

            try
            {
                // Finaly traslate results into a list of documents
                l_oReturnLong = MessageFlowTranslator.TranslateStateDocs(DA_MessageFlow.GetStateDocList());
            }
            catch (Exception ex)
            {
                var l_oStateMessageFlow = new StateDoc("", ex);
                LogManager.Instance().LogError(l_oStateMessageFlow.FullErrorMessage);
                l_oReturnLong.Add(l_oStateMessageFlow);
            }

            return l_oReturnLong;
        }

        /// <summary>
        /// Delete a list msg flow
        /// </summary>
        /// <param name="p_oMsgFlowIds">List of identifier of message flow</param>
        public static ReturnObject DeleteMessageFlow(List<long> p_oMsgFlowIds)
        {
            var l_oRet = new ReturnObject();
            try
            {
                // Delete a msg flow 
                DA_MessageFlow.DeleteMessageFlow(p_oMsgFlowIds);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnObject("(p_oMsgFlowIds<" + p_oMsgFlowIds + ">)", ex);
                LogManager.Instance().LogError(l_oRet.FullErrorMessage);
            }
            return l_oRet;
        }

        /// <summary>
        /// Delete list msg flow from user
        /// </summary>
        /// <param name="p_oMsgFlowId">List of msg flow ID</param>
        /// <param name="p_oRguId">List of Rguid</param>
        public static ReturnObject DeleteMessageFlowUser(List<long> p_oMsgFlowId, List<long> p_oRguId)
        {
            var l_oRet = new ReturnObject();
            try
            {
                // Delete a user msg flow 
                DA_MessageFlow.DeleteMessageFlowUser(p_oMsgFlowId, p_oRguId);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnObject("(p_oMsgFlowId<" + p_oMsgFlowId + "> p_oRguId<" + p_oRguId + ">)", ex);
                LogManager.Instance().LogError(l_oRet.FullErrorMessage);
            }
            return l_oRet;
        }

        /// <summary>
        /// Generate a valid chrono
        /// </summary>
        /// <param name="p_lChronoTypeId">Chrono type identifier</param>
        /// <param name="p_szWordingChrono">Wording identifier</param>
        /// <param name="p_iCreationVolee">0(needed to create) otherwise 1</param>
        /// <returns>The full chrono identifier</returns>
        public static Tuple<long, string> GenerateChrono(long p_lChronoTypeId, string p_szWordingChrono, int p_iCreationVolee, long p_lUserId)
        {
            Tuple<long, string> l_oRet = null;
            try
            {
                // Retreive a valid chrono 
                l_oRet = DA_MessageFlow.RequestChrono(p_szWordingChrono, p_lChronoTypeId, p_iCreationVolee, p_lUserId);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(ex.StackTrace);
            }
            return l_oRet;
        }

        /// <summary>
        /// Update the MSG_FLOW_LAST_READ_DATE field of the message flow table in order to be 
        /// notified when a new action is done on the message
        /// </summary>
        /// <param name="p_lMessageFlowId">Message flow to be updated</param>
        /// <returns></returns>
        public static ReturnObject UpdateMessageFlowLastReadDate(long p_lMessageFlowId)
        {
            var l_oRet = new ReturnObject();
            try
            {
                // Delete a user msg flow 
                DA_MessageFlow.UpdateMessageFlowLastRead(p_lMessageFlowId);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnObject("(p_lMessageFlowId<" + p_lMessageFlowId + ">)", ex);
                LogManager.Instance().LogError(l_oRet.FullErrorMessage);
            }
            return l_oRet;
        }

        /// <summary>
        /// Release chrono
        /// </summary>
        /// <param name="p_sChronoName"></param>
        public static ReturnBoolean ReleaseChrono(string p_sChronoName)
        {
            var l_oReturnBoolean = new ReturnBoolean();
            try
            {
                DA_MessageFlow.UpdateChrono(p_sChronoName, 2);
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("Error in ReleaseChrono (p_sChronoName<" + p_sChronoName + ">) -> Exception", new Exception(ex.Message));
                LogManager.Instance().LogError(l_oReturnBoolean.FullErrorMessage);
            }

            return l_oReturnBoolean;
        }

        /// <summary>
        /// Fix Chrono
        /// </summary>
        /// <param name="p_sChronoName"></param>
        public static ReturnBoolean FixChrono(string p_sChronoName)
        {
            var l_oReturnBoolean = new ReturnBoolean();
            try
            {
                DA_MessageFlow.UpdateChrono(p_sChronoName, 3);
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("Error in FixChrono (p_sChronoName<" + p_sChronoName + ">) -> Exception", new Exception(ex.Message));
                LogManager.Instance().LogError(l_oReturnBoolean.FullErrorMessage);
            }

            return l_oReturnBoolean;
        }

        /// <summary>
        /// Return all message flow for a doc id
        /// </summary>
        /// <param name="p_lDocId"></param>
        /// <returns></returns>
        private static List<MessageFlow> GetMessageFlowByDocId(long p_lDocId)
        {
            var l_oMessageFlowList = new List<MessageFlow>();

            try
            {
                // Finaly traslate results into a list of documents
                l_oMessageFlowList = MessageFlowTranslator.TranslateMessageFlows(DA_MessageFlow.GetMessageFlowIDByDocId(p_lDocId));
            }
            catch (Exception ex)
            {
                var l_oMessageFlow = new MessageFlow("", ex);
                LogManager.Instance().LogError(l_oMessageFlow.FullErrorMessage);
                l_oMessageFlowList.Add(l_oMessageFlow);
            }

            return l_oMessageFlowList;
        }

        /// <summary>
        /// Add senders to an existing message flow
        /// </summary>
        /// <param name="p_lSenders"></param>
        /// <param name="p_lMessageFlowId"></param>
        /// <returns></returns>
        public static ReturnObject AddSenderToMessageFlow(List<long> p_lSenders, long p_lMessageFlowId)
        {
            var l_oRet = new ReturnObject();
            try
            {
                // Delete a user msg flow 
                DA_MessageFlow.AddSenderToMessageFlow(p_lSenders, p_lMessageFlowId);
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnObject("(p_lMessageFlowId<" + p_lMessageFlowId + ">)", ex);
                LogManager.Instance().LogError(l_oRet.FullErrorMessage);
            }
            return l_oRet;
        }
        #endregion
    }
}